<template>
  <basic-container>
    <div class="avue-crud">
      <el-form :inline="true">
        <el-form-item>
          <el-button
            v-if="permissions.river_sysindex_add"
            icon="el-icon-plus"
            type="primary"
            @click="addOrUpdateHandle(false)">
            添加
          </el-button>
        </el-form-item>
      </el-form>

      <el-table
        border
        v-loading="loading"
        :data="indexList"
        row-key="id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildrens' }">
        <el-table-column prop="name" label="指标名称" :show-overflow-tooltip="true" width="180"></el-table-column>
        <el-table-column prop="indexCode" label="指标code" :show-overflow-tooltip="true" width="180"> </el-table-column>
        <el-table-column prop="indexDescription" label="指标介绍" :show-overflow-tooltip="true" width="180"> </el-table-column>
        <el-table-column prop="supportMaterial" label="支撑材料说明" :show-overflow-tooltip="true" width="180"> </el-table-column>
        <el-table-column prop="sortOrder" label="排序" width="60"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-plus"
              @click="addOrUpdateHandle(false, scope.row.id)"
              v-if="permissions.river_sysindex_add"
              >添加
            </el-button>
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="addOrUpdateHandle(true, scope.row.id)"
              v-if="permissions.river_sysindex_edit"
              >修改
            </el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-if="permissions.river_sysindex_del"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList"></table-form>
    </div>
  </basic-container>
</template>

<script>
import { getTree, delObj } from "@/api/sysindex"
import TableForm from "./index-form"
import { mapGetters } from "vuex"

export default {
  name: "Menu",
  components: { TableForm },
  data() {
    return {
      addOrUpdateVisible: false,
      // 遮罩层
      loading: true,
      // 菜单表格树数据
      menuList: [],
      indexList: [],
      // 菜单树选项
      menuOptions: [],
    }
  },
  created() {
    this.getList()
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  methods: {
    addOrUpdateHandle(isEdit, id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(isEdit, id)
      })
    },
    getList() {
      this.loading = true
      getTree().then((res) => {
        this.indexList = res.data.data
        console.log(this.indexList, 777)
        this.loading = false
      })
    },
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.name + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delObj(row.id)
        })
        .then(() => {
          this.getList()
          this.$message.success("删除成功")
        })
    },
  },
}
</script>
